export ALIEN_PACKAGES_AFS_DIR="/pool/PROOF-AAF/alien_packages"

export ALIEN_DIR="/pool/PROOF-AAF/alien"

function cleanVariables() {
unset ALICE
unset ALICE_ARCH
unset ALICE_BIN
unset ALICE_CONF
unset ALICE_DOMAIN_CONF
unset ALICE_INC
unset ALICE_LEVEL
unset ALICE_LIB
unset ALICE_MAN
unset ALICE_ROOT
unset ALICE_SYS
unset ALICE_TARGET
unset ALICE_VERSION

# export PATH="/usr/local/bin:/usr/local/bin/X11:/usr/bin:/bin:/usr/bin/X11:/cern/pro/bin:/usr/kerberos/bin"
# export USERPATH="/usr/local/bin:/usr/local/bin/X11:/usr/bin:/bin:/usr/bin/X11:/cern/pro/bin:/usr/kerberos/bin"
# export GROUPPATH="/usr/local/bin:/usr/local/bin/X11:/usr/bin:/bin:/usr/bin/X11:/cern/pro/bin:/usr/kerberos/bin"

}

cleanVariables


ROOT_LATEST=`ls -al  $ALIEN_PACKAGES_AFS_DIR/VO_ALICE/ROOT/ | grep -v dbg | grep "drwxr" | tail -n 1 | awk '{print $9}'`
ALIROOT_LATEST=`ls -al  $ALIEN_PACKAGES_AFS_DIR/VO_ALICE/AliRoot/ | grep "drwxr" | grep -v "AN" | tail -n 1 | awk '{print $9}'`
ALIROOT_AN_LATEST=`ls -al $ALIEN_PACKAGES_AFS_DIR/VO_ALICE/AliRoot/  | grep "drwxr" | grep "AN" | tail -n 1 | awk '{print $9}'`



if [ -z "$2" ];then
  ROOT_TAG="VO_ALICE@ROOT::$ROOT_LATEST"
else 
  if [ "$1" == "PRO" ];then
  ROOT_TAG="VO_ALICE@ROOT::$ROOT_LATEST"
  else
    ROOT_TAG="$2"
  fi
fi

if [ -z "$1" ];then
  ALICROOT_TAG="VO_ALICE@AliRoot::$ALIROOT_LATEST"
else 
  if [ "$1" == "PRO" ];then
    ALICROOT_TAG="VO_ALICE@AliRoot::$ALIROOT_LATEST"
  else
  ALICROOT_TAG="$1"
  fi
fi


ROOT_TAG=${ROOT_TAG/@/\/} 
ROOT_TAG=${ROOT_TAG/::/\/}

ALICROOT_TAG=${ALICROOT_TAG/@/\/}
ALICROOT_TAG=${ALICROOT_TAG/::/\/}


export ROOTSYS="$ALIEN_PACKAGES_AFS_DIR/$ROOT_TAG/`basename $ROOT_TAG`"
export LD_LIBRARY_PATH="$ROOTSYS/lib:$ALIEN_DIR/api/lib:$ALIEN_DIR/lib:$LD_LIBRARY_PATH"
export PATH="$ROOTSYS/bin:$PATH"
#export PATH="$ROOTSYS/bin:$ALIEN_DIR/api/bin:$ALIEN_DIR/bin:$PATH"
export X509_CERT_DIR="$ROOTSYS/share/certificates"


export ALICE_ROOT="$ALIEN_PACKAGES_AFS_DIR/$ALICROOT_TAG/`basename $ALICROOT_TAG`"

export ALICE_TARGET=`root-config --arch`
export LD_LIBRARY_PATH=$ALICE_ROOT/lib/tgt_${ALICE_TARGET}:$LD_LIBRARY_PATH
export PATH=$ALICE_ROOT/bin/tgt_${ALICE_TARGET}:$PATH
export ALICE="$ALIEN_PACKAGES_AFS_DIR/$ALICROOT_TAG/"
# export ALICE_BIN="$ALICE_ROOT/bin/$ALICE_TARGET"
# export ALICE_CONF=""
# export ALICE_DOMAIN_CONF=""
# export ALICE_INC="$ALICE_ROOT/inc"
export ALICE_LEVEL="`basename $ALICROOT_TAG`"
# export ALICE_LIB="$ALICE_ROOT/lib/tgt_$ALICE_TARGET"
# export ALICE_MAN="$ALICE_ROOT/man"
# export ALICE_SYS="amd64_linux26"
export ALICE_VERSION=""

alien-token-init aliprod
if [ "$?" == "0" ]; then
    echo "alien-token-init OK"
else
    echo "alien-token-init Failed"
fi
echo ""
echo "Latest available versions: "
echo "     ROOT         : $ROOT_LATEST"
echo "     AliRoot      : $ALIROOT_LATEST"
echo "     AliRoot AN   : $ALIROOT_AN_LATEST"
echo ""

echo "Currently running : ROOT    `basename $ALIEN_PACKAGES_AFS_DIR/$ROOT_TAG` $ROOTSYS"
echo "                  : AliROOT `basename $ALIEN_PACKAGES_AFS_DIR/$ALICROOT_TAG` $ALICE_ROOT"
echo ""
# echo "ROOTSYS   is $ROOTSYS"
# echo "ALIEN_DIR is $ALIEN_DIR"
